import config from '@/lib/config.js'
import util from '@/lib/util.js';
import gui from '@/lib/gui.js';

function post(page, url, data, callback){
	if(page.loadingCount == null){
		page.loadingCount = 1;
	}else{
		page.loadingCount = page.loadingCount + 1;
	}
	uni.showLoading({
		title: '请稍候...'
	})
	console.log(config.config().httpserver + url)
	uni.request({
	    url: config.config().httpserver + url,
	    data: data,
	    header: {
	      'content-type': 'application/x-www-form-urlencoded',
		  'token': util.getToken()
	    },
	    success: function (resKey) {
			if (resKey.data.result == 1) {
				if(callback!=null){
					callback(resKey);
				}
			}else{
				gui.showError(resKey.data.msg); 
			}
		}, 
		fail: function(e){
			gui.showError('访问失败');
		},
		complete: function(){
			page.loadingCount=page.loadingCount - 1;
			if(page.loadingCount<=0){
				uni.hideLoading();
			}
			uni.hideLoading();
			uni.stopPullDownRefresh();
		}
	})
}

function mutePost(url, data, callback){
	uni.request({
	    url: config.httpserver + url,
	    data: data,
	    header: {
	      'content-type': 'application/x-www-form-urlencoded'
	    },
	    success: function (resKey) {
			console.log(resKey);
			if (resKey.data.result == 1) {
				if(callback!=null){
					callback(resKey);
				}
			}else{
				gui.showError(resKey.data.msg); 
			}
		}, 
		fail: function(e){
			gui.showError('访问失败');
		}
	})
}

export default{
	post,
	mutePost
}
